﻿using System;
using System.IO;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.OS;
using Java.IO;
using Java.Net;
using NetDimension.Weibo;
using Newtonsoft.Json;

namespace weiboShare.demo
{
    [Activity(Label = "weiboShare.demo", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        private EditText txtShare;
        private Button btnShare;
        private Button btnSharePic;

        private bool IsAuth = false;

        private string AccessToken = "";

        private OAuth weiboOAuth;
        private Client weiboClient;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            txtShare = FindViewById<EditText>(Resource.Id.txtShare);
            btnShare = FindViewById<Button>(Resource.Id.btnShare);
            btnSharePic = FindViewById<Button>(Resource.Id.btnSharePic);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

            btnShare.Click += new EventHandler(btnShare_Click);
            btnSharePic.Click += new EventHandler(btnSharePic_Click);

            CheckAuth();
            CheckClient();

        }

        void btnSharePic_Click(object sender, EventArgs e)
        {
            ShareWeiboPicUrl(txtShare.Text, "xizang.jpg","<a href='http://blog.tenmini.com/user1/208879.html'>疼疼疼</a>");
        }

        void btnShare_Click(object sender, EventArgs e)
        {
            ShareWeiboText(txtShare.Text);
        }

        private void CheckAuth()
        {
            if (!IsAuth)
            {
                weiboOAuth = new OAuth(PlatConstants.WeiboAppKey, PlatConstants.WeiboAppSec);
                //weiboOAuth.AccessToken
                if (weiboOAuth.ClientLogin("rock_82@126.com", "ricted007"))
                {
                    IsAuth = true;
                    AccessToken = weiboOAuth.AccessToken;
                }
                else
                {
                    IsAuth = false;
                    Toast.MakeText(this, "微博授权失败", ToastLength.Short).Show();
                }
            }

        }

        void ShareWeiboText(string msg)
        {
            CheckClient();

            var ret = weiboClient.API.Statuses.Update(msg);
            Log.Debug("ShareWeibo: 文本:", JsonConvert.SerializeObject(ret));
        }

        void ShareWeiboPic(string msg, string picname)
        {
            CheckClient();

            byte[] pic = GetImageBytes(picname);
            if (pic != null)
            {
                var ret = weiboClient.API.Statuses.Upload(msg, pic);
                pic = null;
                Log.Debug("ShareWeibo: 图片:", JsonConvert.SerializeObject(ret));
            }
            else
            {
                Log.Debug("ShareWeibo: 图片:", "分享失败");
            }
        }

        void ShareWeiboPicUrl(string msg, string picname,string url)
        {
            CheckClient();
            msg += url;

            byte[] pic = GetImageBytes(picname);
            if (pic != null)
            {
                var ret = weiboClient.API.Statuses.Upload(msg, pic);
                pic = null;
                Log.Debug("ShareWeibo: 图片:", JsonConvert.SerializeObject(ret));
            }
            else
            {
                Log.Debug("ShareWeibo: 图片:", "分享失败");
            }
        }

        private void CheckClient()
        {
            if (IsAuth)
            {
                if (weiboClient == null)
                {
                    weiboClient = new Client(weiboOAuth);
                }
            }
        }

        byte[] GetImageBytes(string assetsName)
        {
            var assetsManager = this.Assets;
            byte[] picbytes = null;
            try
            {
                Bitmap img;
                using (var stream = assetsManager.Open(assetsName))
                {
                    img = BitmapFactory.DecodeStream(stream);
                }

                picbytes = Bitmap2Bytes(img);
            }
            catch (Exception ex)
            {
                Log.Debug("ReadPicBytes: ", ex.Message);
            }

            return picbytes;
        }

        public byte[] Bitmap2Bytes(Bitmap bm)
        {
            byte[] tmp;
            using (MemoryStream ms = new MemoryStream())
            {

                 bm.Compress(Bitmap.CompressFormat.Png, 100,ms);
                tmp=new byte[ms.Length];
                tmp= ms.ToArray();
            }

            return tmp;
        }
    }
}

